/*
 * sound.c
 *
 * Created: 15.01.2014 4:27:13
 *  Author: Vladislav
 */ 
#include "sound.h"
#include "includes.h"

uint8_t PlaySound(const uint8_t src,const uint8_t snum);
uint8_t WaitSoundQueue(void);
uint8_t PlayNumber(const uint8_t number);
uint8_t McuOutF (uint8_t Cn, uint8_t Bt);

uint8_t McuOutF (uint8_t Cn, uint8_t Bt)
{
	uint8_t Ret;
	OSTimeDlyHMSM(0,0,0,2);
	PORTD.OUTCLR=0x10; // ss - active
	SPID.DATA=Cn;
	while (!(SPID.STATUS&0x80)) {}
	OSTimeDlyHMSM(0,0,0,2);
	SPID.DATA=Bt;
	while (!(SPID.STATUS&0x80)) {}
	
	Ret=SPID.DATA;
	PORTD.OUTSET=0x10; // ss - passive
	return Ret;
}


uint8_t WaitSoundQueue()
{
	uint8_t Ret;
	
	PORTD.OUTCLR=0x10; // ss - active
	SPID.DATA=4;
	while (!(SPID.STATUS&0x80)) {}

	OSTimeDlyHMSM(0,0,0,2);
	
	SPID.DATA=0;
	while (!(SPID.STATUS&0x80)) {}
	
	Ret=SPID.DATA;
	PORTD.OUTSET=0x10; // ss - passive
	return Ret;
}

uint8_t PlaySound(const uint8_t src,const uint8_t snum)
{
	uint8_t ret;
	OSTimeDlyHMSM(0,0,0,2);
	
	ret = McuOutF(src,snum);
	
	return ret;
}




uint8_t PlayNumber(const uint8_t number)
{
	uint8_t num = 0;
	uint8_t ret = 0;
	

	switch(number)
	{
		case 1:
		num = MSG_1;
		
		break;
		case 2:
		num = MSG_2;
		break;
		case 3:
		num = MSG_3;
		break;
		case 4:
		num = MSG_4;
		break;
		
		case 5:
		num = MSG_5;
		
		break;
		case 6:
		num = MSG_6;
		break;
		case 7:
		num = MSG_7;
		break;
		case 8:
		num = MSG_8;
		break;
		
		case 9:
		num = MSG_9;
		
		break;
		case 10:
		num = MSG_10;
		break;
		case 11:
		num = MSG_11;
		break;
		case 12:
		num = MSG_12;
		break;
		
		case 13:
		num = MSG_13;
		
		break;
		case 14:
		num = MSG_14;
		break;
		case 15:
		num = MSG_15;
		break;
		case 16:
		num = MSG_16;
		break;
		
		case 17:
		num = MSG_17;
		
		break;
		case 18:
		num = MSG_18;
		break;
		case 19:
		num = MSG_19;
		break;
		case 20:
		num = MSG_20;
		break;
		case 21:
		num = 168;
		break;
		case 22:
		num = 169;
		break;
		case 23:
		num = 170;
		break;
		case 24:
		num = 171;
		break;
		case 25:
		num = 172;
		break;
		case 26:
		num = 173;
		break;
		case 27:
		num = 174;
		break;
		case 28:
		num = 175;
		break;
		case 29:
		num = 176;
		break;
		case 30:
		num = 177;
		break;
	}
	
	ret = PlaySound(SPEAKER,num);
	
	return ret;
}

